<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>费用科目</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<jsp:include page="${pageContext.request.contextPath }/resource_path.jsp" flush="true"/>
</head>
<body class="layui-anim layui-anim-up">
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a>
          <cite>费用科目</cite>
        </a>
      </span>
    </div>
    <div class="x-body">
      <div style="margin-bottom: 18px">
		<div class="layui-inline layui-form">
			<table>
				<tr>
					<td>
						<input class="layui-input" id="keyword01" placeholder="请输入科目名称" autocomplete="off" style="display: inline; width: 180px; margin-left: 10px">
					</td>
					<td>
						<div class="layui-input-block" style="width:180px;margin-left: 10px;">
							<select class="layui-input" id="keyword02" lay-search>
									<option value="">请选择类型</option>
									<option value="0">物料费用</option>
									<option value="1">供应商费用</option>
							</select>
						</div>
					</td>
				</tr>
			</table>
		</div>
		<button class="layui-btn layui-btn-normal" data-type="reload"><i class="layui-icon">&#xe615;</i>检索</button>
	  </div>
      <xblock>
			<button id="add" class="layui-btn layui-btn-warm"><i class="layui-icon"></i>新增</button>
			<button id="dels" class="layui-btn layui-btn-danger"><i class="layui-icon layui-icon-delete"></i>批量删除</button>
			<button id="enable" class="layui-btn layui-btn-danger"><i class="layui-icon layui-icon-ok"></i>启用</button>
			<button id="prohibit" class="layui-btn layui-btn-danger"><i class="layui-icon layui-icon-close"></i>禁用</button>
      </xblock> 
	  <table class="layui-hide" id="tableList" lay-filter="tableList"></table>
	</div>
	<script type="text/html" id="rowToolbar">
		<a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
	</script>

	<div id="addDivID" hidden="hidden">
		<form class="layui-form" id="addFormID">
			<xblock>
				<table style="margin-left: 15px;">
					<tr>
						<td>
							<label class="layui-form-label">名称</label></td>
						<td>
							<input class="layui-input" id="ename" name="ename" lay-verify="only" style="width: 280px;margin-top: 0px;" placeholder="请输入名称">
							<input type="hidden" id="eid" name="eid">
							<span class="xing" style="margin-left: 101%;"></span>
						</td>
					</tr>
                   	<tr style="height: 10px">
                   	</tr>
					<tr>
						<td>
							<label class="layui-form-label">计价单位</label></td>
						<td>
							<input class="layui-input" id="eunit" name="eunit" lay-verify="required" style="width: 280px" placeholder="请输入计价单位">
							<span class="xing" style="margin-left: 101%;"></span>
						</td>
					</tr>
                   	<tr style="height: 10px">
                   	</tr>
					<tr>
						<td>
							<label class="layui-form-label">类型</label></td>
						<td>
							<div class="layui-input-block" style="width:280px;margin-left: 0px;margin-top: 0px;">
								<select class="layui-input" name="etype" id="etype" lay-verify="required">
										<option value="0">物料费用</option>
										<option value="1">供应商费用</option>
								</select>
							</div>
							<span class="xing" style="margin-left: 101%;"></span>
						</td>
					</tr>
					<tr style="height:20px">
					</tr>
				</table>
			</xblock>
			<xblock>
				<button class="layui-btn layui-btn-blue" id="subBtn" lay-submit lay-filter="addForm" style="margin-left:150px; margin-bottom: 10px">立即提交</button>
				&emsp;&emsp;&emsp;&emsp;
				<button class="layui-btn layui-btn-primary" style="margin-bottom: 10px">取消</button>
			</xblock>
			</table>
		</form>
    </div>

	<script type="text/javascript">
        var table;
		layui.use(['table','layer','upload','form','laydate'], function(){
			table = layui.table;
			var layer = layui.layer;
			var form = layui.form;
			
			
			
			var laydate = layui.laydate;
			var $ = layui.jquery, active = {
				reload:function () {
					var keyword01 = $.trim($("#keyword01").val());
	                var keyword02 = $.trim($("#keyword02").val());
					table.reload('tableList',{
						method:'get'
						,where:{
							ename:keyword01
	                        ,etype:keyword02
						}
						,page: {
		                    curr: 1//重新从第 1 页开始
		                }
					});
				}
			}
			$('.layui-btn').on('click', function(){
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});
			var url = '${pageContext.request.contextPath }/saleExpenseAccount/list.do'
			table.render({
				elem: '#tableList'
				,url:url
				,toolbar: '#toolbar'
				,title: 'machineList'
				,limits:[10,20,30]
                ,defaultToolbar:['filter', 'print']
				,request: {   //如果无需自定义请求参数，可不加该参数
	                pageName: 'page' //页码的参数名称，默认：page
	                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
	            }
	            ,response: {   //如果无需自定义数据响应名称，可不加该参数
	                countName: 'total' //规定数据总数的字段名称，默认：count
	                ,dataName: 'rows' //规定数据列表的字段名称，默认：data
	            }
				,cols: [
		                [{
		                    type: 'checkbox',
		                    fixed: 'left'
		                }, {
		                	field:'eid',
		                	title:'序号', 
		                	sort: true,
		                	width: 60,
		                	type:'numbers',
		                	align:'center'
		                }, {
		                    field: 'eid',
		                    title: 'id',
		                    hide:true //是否初始隐藏列，默认：false。layui 2.4.0 新增
		                } , {
		                    field: 'ename',
		                    title: '名称',
		                    align:'center',
		                }, {
		                    field: 'eunit',
		                    title: '计价单位',
		                    align:'center',
		                }, {
		                    field: 'etype',
		                    title: '类型',
		                    align:'center'
	                    	,templet: function(d){
		                    	var value = d.etype;
            	    			if(value=="0"){
            	    				value = "物料费用"
            	    			}else if(value=="1"){
            	    				value = "供应商费用"
            	    			}
            	    			return value;
            	    		}
		                }, {
		                    field: 'eisTrue',
		                    title: '是否启用',
		                    align:'center'
		                    ,templet: function(d){
		                    	var value = d.eisTrue;
            	    			if(value=="0"){
            	    				value = "是"
            	    			}else if(value=="1"){
            	    				value = "否"
            	    			}
            	    			return value;
            	    		}
		                }, {
		                    field: 'ecreateMan',
		                    title: '创建人',
		                    align:'center',
		                }, {
		                    field: 'ecreateTime',
		                    title: '创建时间',
		                    align:'center',
		                }, {
		                    fixed: 'right',
		                    title:'操作',
		                    toolbar: '#rowToolbar',
		                    align:'center',
		                    unresize: true,
		                    width: 150 
		                }
		                ]
		               ]
				,page: true
				,done : function(){
	                $('th').css({
	                    'background-color': '#009688', 'color': '#fff','font-weight':'bold',
	                    //'font-size': 10,
	                })
	            }
			});
        	
	        table.on('row(tableList)', function(obj){
	        	if(obj.checked == true && obj.type == 'all'){
	                //点击全选
	                $('.layui-table-body table.layui-table tbody tr').addClass('layui-table-click');
	            }else if(obj.checked == false && obj.type == 'all'){
	                //点击全不选
	                $('.layui-table-body table.layui-table tbody tr').removeClass('layui-table-click');
	            }else if(obj.checked == true && obj.type == 'one'){
	                //点击单行
	                if(obj.checked == true){
	                    obj.tr.addClass('layui-table-click');
	                }else{
	                    obj.tr.removeClass('layui-table-click');
	                }
	            }else if(obj.checked == false && obj.type == 'one'){
	                //点击全选之后点击单行
	                if(obj.tr.hasClass('layui-table-click')){
	                    obj.tr.removeClass('layui-table-click');
	                }
	            }
	        });
	        
			//监听行工具事件
			table.on('tool(tableList)', function(obj){
				var data = obj.data;
				 if(data.eisTrue==0){
              	   
              	   toastr.warning("已启用不能编辑！");
              	   
                 }else{
				var idArr = new Array();
				if(obj.event === 'edit'){
					layer.open({
					  type: 1 							//Page层类型
					  ,area: ['500px', ''] 	//宽  高
					  ,title: '编辑'
					  ,shade: 0.6 						//遮罩透明度
					  ,maxmin: true 					//允许全屏最小化
					  ,anim: 1 							//0-6的动画形式，-1不开启
					  ,content: $('#addDivID')
	                  ,end: function () {
	                      var formDiv = document.getElementById('addDivID');
	                      formDiv.style.display = '';
	                  }
	                  ,success: function(){
	                      $("#eid").val(data.eid)
	                      $("#ename").val(data.ename)
	                      $("#eunit").val(data.eunit)
	                      $("#etype").val(data.etype)
	                      form.render();
	                    }
					});
				}
                 }
			});
			/**
			 * 通用表单提交(AJAX方式)(新增)
			 */
			form.on('submit(addForm)', function (data) {
				var index = layer.load(1, {
	        	    shade: [0.1,'#000'] //0.1透明度的背景
	        	});
				
				var formData = $("#addFormID").serialize();
				var title = "新增";
				var url = '${pageContext.request.contextPath}/saleExpenseAccount/insert.do';
				var eid = $.trim($("#eid").val());
				if(eid != "" && eid != null && eid != 'undefined'){
					var url = '${pageContext.request.contextPath}/saleExpenseAccount/update.do';
					title = "修改";
				}
				$.ajax({
					url : url,
					data: formData,
					cache : false,
					type : "post",
					success: function (data) {
	                    if (data >= 1) {
	                        toastr.success(title+'成功');
	                        setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
	                            window.location.reload();
	                            $("#reset").click();
	                        },1000);
	                    } else {
	                        toastr.error(title+'失败，该费用科目名称已存在，不能重复');
	                    }
	                },
				})
				layer.close(index);
				return false;
			});

			

			/**
			 * 新增表单校验
			 */
			form.verify({
				//value：表单的值、item：表单的DOM对象
	            required: function(value, item){
	            	value = $.trim(value)
	            	if(value == '' || value == null){
	                    return '不能为空';
	                }
	            },
				//value：表单的值、item：表单的DOM对象
	            only: function(value, item){
	            	value = $.trim(value)
	            	if(value == '' || value == null){
	                    return '不能为空';
	                }else{
	                    var flg = false;
	                    $.ajax({
	                        type:'post',
	                        data:'ename='+value,
	                        url:'${pageContext.request.contextPath}/saleExpenseAccount/queryAllByMution.do',
	                        dataType: 'JSON',
	                        async: false,
	                        success:function (data) {
	                            if(data.length == 0){
	                                flg  = true;
	                            }else if(data.length == 1){
	                                var ma = $.trim($("#eid").val());
	                                if (ma == data[0].eid) {
	                                    flg  = true;
	                                }else{
	                                    flg = false;
	                                }
	                            }else{
	                                flg = false;
	                            }
	
	                        }
	                    });
	                    if(!flg){
	                        return '科目名称不能重复';
	                    }
	                }
	            }
			});

			
			//增加
            $("#add").click(function(){
                layer.open({
                    type: 1 					//Page层类型
                    ,area: ['500px', ''] 	//宽  高
                    ,title: '新增'
                    ,shade: 0.6 				//遮罩透明度
                    ,maxmin: true 				//允许全屏最小化
                    ,anim: 1 					//0-6的动画形式，-1不开启
                    ,content: $('#addDivID')
                    ,end: function () {
                        var formDiv = document.getElementById('addDivID');
                        formDiv.style.display = '';
                    }
                    ,success: function(){
                    	$("#eid").val("")
                    	$("#ename").val("")
                    	$("#eunit").val("")
                    	$("#etype").val("")
                        form.render();
                    }
                });
			});

			

          	//批量删除
			$("#dels").click(function(){
				var checkStatus = table.checkStatus('tableList')
	      		var data = checkStatus.data;
	            var idArr = new Array();
	            if(data.length==0){
	                toastr.warning("请至少选择一条记录！");
	            }else {
	            	for(var i=0;i<data.length;i++){
	                   if(data[i].eisTrue==0){
	                	   
	                	   toastr.warning("已启用不能删除！");
	                	   
	                   }else{
	                	   
	                	   var ids = "";
	   	                for(var i=0;i<data.length;i++){
	   	                    if(ids == ""){
	   	                        ids += data[i].eid;
	   	                    }else{
	   		                    ids += "," + data[i].eid;
	   	                    }
	   	                }
	   	                if(ids == ""){
	   	                	toastr.warning("请至少选择一条记录！！");
	   	                }else{
	   		                layer.confirm('确定删除吗？', function(index){
	   		                    $.ajax({
	   		                        type:'post',
	   		                        url:'${pageContext.request.contextPath }/saleExpenseAccount/delete.do',
	   		                        data:{"ids":ids},
	   		                        success:function(data){
	   		                            if(data>0){
	   		                                toastr.success("删除成功！");
	   		                                setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
	   		                                    //关闭模态框
	   		                                    // 父页面刷新
	   		                                    window.location.reload();
	   		                                },2000);
	   		                            }else{
	   		                                toastr.warning("删除失败！");
	   		                            }
	   		                        }
	   	
	   		                    })
	   		                    layer.closeAll();
	   		                });
	   	                }
	                	   
	                	   
	                   }
	                    }
	            	
	            }
	            
	         
			});
          	//批量启用
			$("#enable").click(function(){
				var checkStatus = table.checkStatus('tableList')
	      		var data = checkStatus.data;
	            var idArr = new Array();
	            if(data.length==0){
	                toastr.warning("请至少选择一条记录！");
	            }else{
	                var names = "";
	                var ids = "";
	                for(var i=0;i<data.length;i++){
	                    if(data[i].eisTrue == 0){
	                    	names = names+data[i].ename+","
	                    }else{
	                    	if(ids == ""){
		                        ids += data[i].eid;
		                    }else{
			                        ids += "," + data[i].eid;
		                    }
	                    }
	                }
	                if(names != ""){
	                	names =  "已启用："+names+"还有其他可启用，"
	                }
	                if(ids == ""){
	                	toastr.warning("所选数据已启用！！");
	                }else{
		                layer.confirm(names+'确定启动吗？', function(index){
		                    $.ajax({
		                        type:'post',
		                        url:'${pageContext.request.contextPath }/saleExpenseAccount/updateEisTrueByIds.do',
		                        data:{"ids":ids,"eisTrue":0},
		                        success:function(data){
		                            if(data>0){
		                                toastr.success("启用成功！");
		                                setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
		                                    //关闭模态框
		                                    // 父页面刷新
		                                    window.location.reload();
		                                },2000);
		                            }else{
		                                toastr.warning("启用失败！");
		                            }
		                        }
	
		                    })
		                    layer.closeAll();
		                });
	                }
	            }
			});
          	//批量禁用
			$("#prohibit").click(function(){
				var checkStatus = table.checkStatus('tableList')
	      		var data = checkStatus.data;
	            var idArr = new Array();
	            if(data.length==0){
	                toastr.warning("请至少选择一条记录！");
	            }else{
	            	var names = "";
	                var ids = "";
	                for(var i=0;i<data.length;i++){
	                    if(data[i].eisTrue == 1){
	                    	names = names+data[i].ename+","
	                    }else{
	                    	if(ids == ""){
		                        ids += data[i].eid;
		                    }else{
			                        ids += "," + data[i].eid;
		                    }
	                    }
	                }
	                if(names != ""){
	                	names =  "已禁用："+names+"还有其他可禁用用，"
	                }
	                
	                if(ids == ""){
	                	toastr.warning("所选数据已禁用！！");
	                }else{
		                layer.confirm(names+'确定禁用吗？', function(index){
		                    $.ajax({
		                        type:'post',
		                        url:'${pageContext.request.contextPath }/saleExpenseAccount/updateEisTrueByIds.do',
		                        data:{"ids":ids,"eisTrue":1},
		                        success:function(data){
		                            if(data>0){
		                                toastr.success("禁用成功！");
		                                setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
		                                    //关闭模态框
		                                    // 父页面刷新
		                                    window.location.reload();
		                                },2000);
		                            }else{
		                                toastr.warning("禁用失败！");
		                            }
		                        }
	
		                    })
		                    layer.closeAll();
		                });
	                }
	            }
			});
			  
		});
		
		//获取当前时间
	   	function curentTimeNumber() {
	   		var now=new Date();
	   		var year = now.getFullYear();       //年
	   	    var month = now.getMonth() + 1;     //月
	   	    var day = now.getDate();            //日
	   	 	var hours = now.getHours(); //获取当前小时数(0-23)
		   	var minutes = now.getMinutes(); //获取当前分钟数(0-59)
		   	var seconds = now.getSeconds(); //获取当前秒数(0-59)
	   	    //var mill=now.getMilliseconds();
	   	    var time=year+""+add0(month)+""+add0(day)+""+add0(hours)+""+add0(minutes)+""+add0(seconds);
	   		return time;
	   	}
	    //获取当前时间
	   	function curentTime() {
	   		var now=new Date();
	   		var year = now.getFullYear();       //年
	   	    var month = now.getMonth() + 1;     //月
	   	    var day = now.getDate();            //日
	   	    var time=year+"-"+add0(month)+"-"+add0(day);
	   		return time;
	   	}
	   	function add0(m){return m<10?'0'+m:m }
		toastrStyle()
	</script>
</body>
</html>